home *** CD-ROM | disk | FTP | other *** search
- // main.c
-
- #include <A4Stuff.h>
- #include <Resources.h>
- #include <Windows.h>
- #include <Quickdraw.h>
- #include <Traps.h>
- #include <OSUtils.h>
- #include "Offscreen.h"
-
-
- #define Increment 1
- #define PADDLESPEED 3
- #define BALLSPEED 1
-
- #define SLEEPTIME 1
-
- #define WidthCalc 5
- #define HeightCalc 22
-
- #define BricksH 8
- #define BricksV 4
- #define kHit 1
- #define kSafe 0
-
- #define KeyMapLoMem ((unsigned char *)0x174)
- #define KeyIsDown(key) (( KeyMapLoMem[ key >> 3 ] >> ( key & 7)) &1)
-
- #define aKey 0x00
- #define sKey 0x01
- #define fourKey 0x56
- #define fiveKey 0x57
- #define sixKey 0x58
- #define threeKey 0x55
- #define oneKey 0x53
-
- Point pongSpeed;
- Rect ballRect, paddleRectOne, paddleRectTwo;
- GWorldPtr ball = nil;
- GWorldPtr paddleOne = nil, paddleTwo = nil;
- GWorldPtr pongGWorld = nil;
- WindowPtr pongWindow = nil;
- Boolean gTakeOver = TRUE;
- Boolean gDone = FALSE;
-
-
- Point boSpeed;
- Rect boBallRect, boPaddleRect;
- GWorldPtr boBall = nil;
- GWorldPtr boPaddle = nil;
- GWorldPtr brickWorld = nil;
- GWorldPtr boGWorld = nil;
- WindowPtr breakoutWindow = nil;
- Boolean gBOTakeOver = TRUE;
- Boolean gBODone = FALSE;
-
-
-
- unsigned long oldTicks = 0L;
-
- RGBColor white = { 65535, 65535, 65535 };
- RGBColor black = { 0, 0, 0 };
- RGBColor green = { 0 , 17476 , 0 };
- RGBColor blue = { 0 , 0 , 17476 };
- RGBColor red = { 17476 , 0 , 0 };
- RGBColor lightgreen = { 0 , 48059 , 0 };
- RGBColor lightblue = { 13107 , 26214 , 65535 };
- RGBColor lightred = { 34952 , 0 , 0 };
-
- short BrickState[BricksH][BricksV];
- Rect Bricks[BricksH][BricksV];
- short BrickWidth, BrickHeight;
- Rect brickRect, oldBall, oldPaddle;
-
-
- void InitPong(WindowPtr theWindow);
- void DisposePong(void);
- void DoPong(WindowPtr theWindow, EventRecord * theEvent);
- void UpdateBall(WindowPtr window);
- void UpdatePaddles(WindowPtr window, EventRecord * theEvent);
- void CreateBall(WindowPtr window);
- void CreatePaddles(WindowPtr window);
-
-
- void InitBreakOut(WindowPtr theWindow);
- void DisposeBreakOut(void);
- void DoBreakOut(WindowPtr theWindow, EventRecord * theEvent);
- void UpdateBreakoutBall(WindowPtr window);
- void UpdateBreakoutPaddle(WindowPtr window, EventRecord * theEvent);
- void CreateBreakoutBall(WindowPtr window);
- void CreateBreakoutPaddle(WindowPtr window);
- void CreateBricks(WindowPtr window);
- void DrawBricks(WindowPtr theWindow);
- void CheckCollide(WindowPtr theWindow);
-
-
- typedef pascal void (*endUpdate)(WindowPtr theWindow);
- endUpdate gEndUpdateAddr;
- pascal void MyEndUpdate(WindowPtr theWindow);
-
- typedef pascal void (*closeWindow)(WindowPtr theWindow);
- closeWindow gCloseWindowAddr;
- pascal void MyCloseWindow(WindowPtr theWindow);
-
- typedef pascal void (*showHide)(WindowPtr theWindow, Boolean showFlag);
- showHide gShowHideAddr;
- pascal void MyShowHide(WindowPtr theWindow, Boolean showFlag);
-
- typedef pascal void (*drawControls)(WindowPtr theWindow);
- drawControls gDrawControlsAddr;
- pascal void MyDrawControls(WindowPtr theWindow);
-
- typedef pascal Boolean (*getNextEvent)(short mask, EventRecord *theEvent);
- getNextEvent gGetNextEventAddr;
- pascal Boolean MyGetNextEvent(short mask, EventRecord *theEvent);
-
-
- void main(void)
- {
- Handle init;
-
- EnterCodeResource();
-
- init = Get1Resource('INIT', 0);
- if (init == nil)
- {
- DebugStr("\pOh shit");
- ExitCodeResource();
- return;
- }
- DetachResource(init);
-
- gEndUpdateAddr = (endUpdate)NGetTrapAddress(_EndUpDate, ToolTrap);
- NSetTrapAddress((UniversalProcPtr)MyEndUpdate, _EndUpDate, ToolTrap);
-
- gCloseWindowAddr = (closeWindow)NGetTrapAddress(_CloseWindow, ToolTrap);
- NSetTrapAddress((UniversalProcPtr)MyCloseWindow, _CloseWindow, ToolTrap);
-
- gShowHideAddr = (showHide)NGetTrapAddress(_ShowHide, ToolTrap);
- NSetTrapAddress((UniversalProcPtr)MyShowHide, _ShowHide, ToolTrap);
-
- gGetNextEventAddr = (getNextEvent)NGetTrapAddress(_GetNextEvent, ToolTrap);
- NSetTrapAddress((UniversalProcPtr)MyGetNextEvent, _GetNextEvent, ToolTrap);
-
- gDrawControlsAddr = (drawControls)NGetTrapAddress(_DrawControls, ToolTrap);
- NSetTrapAddress((UniversalProcPtr)MyDrawControls, _DrawControls, ToolTrap);
-
- ExitCodeResource();
- }
-
-
-
- pascal void MyCloseWindow(WindowPtr theWindow)
- {
- EnterCodeResource();
-
- if (theWindow == pongWindow)
- {
- DisposePong();
- pongWindow = nil;
- oldTicks = 0L;
- gTakeOver = TRUE;
- gDone = FALSE;
- }
- else if (theWindow == breakoutWindow)
- {
- DisposeBreakOut();
- breakoutWindow = nil;
- oldTicks = 0L;
- gBOTakeOver = TRUE;
- gBODone = FALSE;
- }
- gCloseWindowAddr(theWindow);
-
- ExitCodeResource();
- }
-
-
- pascal void MyShowHide(WindowPtr theWindow, Boolean showFlag)
- {
- EnterCodeResource();
-
- gShowHideAddr(theWindow, showFlag);
-
- if (showFlag == TRUE)
- {
- if (pongWindow == nil)
- {
- if (EqualString(*(((WindowPeek)theWindow)->titleHandle), "\pPONG!", TRUE, TRUE))
- {
- InitPong(theWindow);
- }
- }
- if (breakoutWindow == nil)
- {
- if (EqualString(*(((WindowPeek)theWindow)->titleHandle), "\pWOZ!", TRUE, TRUE))
- {
- InitBreakOut(theWindow);
- }
- }
- }
-
- ExitCodeResource();
- }
-
-
- pascal void MyDrawControls(WindowPtr theWindow)
- {
- EnterCodeResource();
-
- if (theWindow != pongWindow && theWindow != breakoutWindow)
- gDrawControlsAddr(theWindow);
-
- ExitCodeResource();
- }
-
- pascal Boolean MyGetNextEvent(short eventMask, EventRecord *theEvent)
- {
- Boolean result;
- WindowPtr theWindow;
- GrafPtr myPort;
- getNextEvent gne;
- unsigned long ticks;
-
-
- EnterCodeResource();
-
- gne = gGetNextEventAddr;
- ExitCodeResource();
- result = gne(eventMask, theEvent);
- oldA4 = SetCurrentA4();
-
- if (theEvent->what == keyDown && (char) theEvent->message == '*')
- Debugger();
-
-
- theWindow = pongWindow;
-
- if (theWindow != nil/* && theWindow == pongWindow*/)
- {
- if (!EqualRect(&pongGWorld->portRect, &theWindow->portRect))
- DisposePong();
-
- if (pongGWorld == nil)
- InitPong(theWindow);
-
- GetPort(&myPort);
- SetPort((GrafPtr)theWindow);
-
- if (gTakeOver)
- {
- if (gDone && KeyIsDown(threeKey))
- gDone = FALSE;
-
- while (!gDone)
- {
- if (KeyIsDown(oneKey))
- {
- gTakeOver = !gTakeOver;
- gDone = TRUE;
- }
-
- if (KeyIsDown(fiveKey))
- gDone = TRUE;
-
- ticks = TickCount();
-
- if (ticks - oldTicks > 0)
- {
- oldTicks = ticks;
- DoPong(theWindow, theEvent);
- }
-
- EventAvail(0, theEvent);
- }
- }
- else
- DoPong(theWindow, theEvent);
-
- SetPort(myPort);
- }
-
- theWindow = breakoutWindow;
-
- if (theWindow != nil/* && theWindow == pongWindow*/)
- {
- if (!EqualRect(&boGWorld->portRect, &theWindow->portRect))
- DisposeBreakOut();
-
- if (boGWorld == nil)
- InitBreakOut(theWindow);
-
- GetPort(&myPort);
- SetPort((GrafPtr)theWindow);
-
- if (gBOTakeOver)
- {
- if (gBODone && KeyIsDown(threeKey))
- gBODone = FALSE;
-
- while (!gBODone)
- {
- if (KeyIsDown(oneKey))
- {
- gBOTakeOver = !gBOTakeOver;
- gBODone = TRUE;
- }
-
- if (KeyIsDown(fiveKey))
- gBODone = TRUE;
-
- ticks = TickCount();
-
- if (ticks - oldTicks > 0)
- {
- oldTicks = ticks;
- DoBreakOut(theWindow, theEvent);
- }
-
- EventAvail(0, theEvent);
- }
- }
- else
- DoBreakOut(theWindow, theEvent);
-
- SetPort(myPort);
- }
-
- ExitCodeResource();
-
- return result;
- }
-
-
-
- pascal void MyEndUpdate(WindowPtr theWindow)
- {
- GrafPtr oldPort;
-
-
- EnterCodeResource();
-
- GetPort(&oldPort);
-
- gEndUpdateAddr(theWindow);
-
- SetPort((GrafPtr)theWindow);
- if (theWindow == pongWindow)
- {
- RGBForeColor(&black);
- PaintRect(&theWindow->portRect);
- DoPong(theWindow, nil);
- }
- if (theWindow == breakoutWindow)
- {
- RGBForeColor(&black);
- PaintRect(&theWindow->portRect);
- DoBreakOut(theWindow, nil);
- }
- SetPort(oldPort);
-
- ExitCodeResource();
- }
-
-
- #pragma mark -
-
- void DoPong(WindowPtr theWindow, EventRecord * theEvent)
- {
- UpdatePaddles(theWindow, theEvent);
- UpdateBall(theWindow);
- }
-
-
- void UpdatePaddles(WindowPtr theWindow, EventRecord * theEvent)
- {
- Boolean handled = FALSE;
-
-
- if(KeyIsDown(aKey))
- {
- if(paddleRectOne.left > 0)
- OffsetRect(&paddleRectOne, -PADDLESPEED, 0);
- handled = TRUE;
- }
- if(KeyIsDown(sKey))
- {
- if(paddleRectOne.right < theWindow->portRect.right)
- OffsetRect(&paddleRectOne, PADDLESPEED, 0);
- handled = TRUE;
- }
- if(KeyIsDown(fourKey))
- {
- if(paddleRectTwo.left > 0)
- OffsetRect(&paddleRectTwo, -PADDLESPEED, 0);
- handled = TRUE;
- }
- if(KeyIsDown(sixKey))
- {
- if(paddleRectTwo.right < theWindow->portRect.right)
- OffsetRect(&paddleRectTwo, PADDLESPEED, 0);
- handled = TRUE;
- }
- if (KeyIsDown(fiveKey) || KeyIsDown(threeKey) || KeyIsDown(oneKey))
- handled = TRUE;
-
-
-
- RGBBackColor(&white);
- CopyBits( &((GrafPtr)paddleOne)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &paddleOne->portRect,
- &paddleRectOne,
- 0,
- NULL);
-
- RGBBackColor(&white);
- CopyBits( &((GrafPtr)paddleTwo)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &paddleTwo->portRect,
- &paddleRectTwo,
- 0,
- NULL);
-
- if (theEvent != nil && handled)
- {
- theEvent->what = nullEvent;
- }
- }
-
- void UpdateBall(WindowPtr theWindow)
- {
- Rect where;
- short center1,center2,offset1,offset2, offset3;
-
-
- if(ballRect.right >= theWindow->portRect.right)
- pongSpeed.h *= -1;
- if(ballRect.left <= 0)
- pongSpeed.h *= -1;
- if(ballRect.bottom >= theWindow->portRect.bottom)
- pongSpeed.v *= -1;
- if(ballRect.top <= 0)
- pongSpeed.v *= -1;
-
- if(SectRect(&ballRect, &paddleRectOne,&where))
- {
- if(ballRect.bottom-pongSpeed.v <= paddleRectOne.top)
- {
- center1 = where.right / 2;
- center2 = paddleRectOne.right / 2;
-
- offset1 = (theWindow->portRect.right / (WidthCalc*3))+paddleRectOne.left;
- offset2 = offset1+(theWindow->portRect.right / (WidthCalc*3));
- offset3 = offset2+(theWindow->portRect.right / (WidthCalc*3));
-
- if(center1 < offset1)
- pongSpeed.v *= -1,
- pongSpeed.h--;
-
- if(center1 > offset1 && center1 < offset2)
- pongSpeed.v *= -1;
-
- if(center1 > offset2 && center1 < offset3)
- pongSpeed.v *= -1,
- pongSpeed.h++;
- }
- }
- if(SectRect(&ballRect, &paddleRectTwo,&where))
- {
- if(ballRect.top-pongSpeed.v >= paddleRectTwo.bottom)
- {
- center1 = where.right / 2;
- center2 = paddleRectTwo.right / 2;
-
- offset1 = (theWindow->portRect.right / (WidthCalc*3))+paddleRectTwo.left;
- offset2 = offset1+(theWindow->portRect.right / (WidthCalc*3));
- offset3 = offset2+(theWindow->portRect.right / (WidthCalc*3));
-
- if(center1 < offset1)
- pongSpeed.v *= -1,
- pongSpeed.h--;
-
- if(center1 > offset1 && center1 < offset2)
- pongSpeed.v *= -1;
-
- if(center1 > offset2 && center1 < offset3)
- pongSpeed.v *= -1,
- pongSpeed.h++;
- }
- }
- if(pongSpeed.h > PADDLESPEED)
- pongSpeed.h = PADDLESPEED;
- if(pongSpeed.v > PADDLESPEED)
- pongSpeed.v = PADDLESPEED;
- if(pongSpeed.h < -PADDLESPEED)
- pongSpeed.h = -PADDLESPEED;
- if(pongSpeed.v < -PADDLESPEED)
- pongSpeed.v = -PADDLESPEED;
-
- OffsetRect(&ballRect, pongSpeed.h, pongSpeed.v);
-
- RGBBackColor(&white);
- CopyBits( &((GrafPtr)ball)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &ball->portRect,
- &ballRect,
- 0,
- NULL);
- }
-
- GWorldPtr CreateOffscreen(Rect GWorldRect)
- {
- QDErr myQDErr;
- GWorldPtr worldForMe;
-
-
- myQDErr = NewGWorld( &worldForMe, 0, &GWorldRect, NULL, NULL, useTempMem );
- if (myQDErr != noErr)
- {
- DebugStr("\pOh no no memory!");
- return nil;
- }
-
- SetupOffscreen(worldForMe);
-
- return worldForMe;
- }
-
- void SetupOffscreen(GWorldPtr GWorld)
- {
- LockPixels( GetGWorldPixMap( GWorld ) );
- SetPort((GrafPtr)GWorld);
- PaintRect(&GWorld->portRect);
- }
-
-
- void CreatePaddles(WindowPtr theWindow)
- {
- short width,height;
- Rect paddlePortRect, paddleRect;
-
-
- width = (theWindow->portRect.right - theWindow->portRect.left) / WidthCalc;
- height = (theWindow->portRect.bottom - theWindow->portRect.top) / HeightCalc;
-
- SetRect(&paddlePortRect, 0, 0, width + (PADDLESPEED << 1), height);
- paddleRectOne = paddleRectTwo = paddlePortRect;
-
- OffsetRect(&paddleRectOne, theWindow->portRect.right/2-width,theWindow->portRect.bottom-height*2);
- OffsetRect(&paddleRectTwo, theWindow->portRect.right/2-width,theWindow->portRect.top+height);
-
- SetRect(&paddleRect, PADDLESPEED, 0, width + PADDLESPEED, height);
-
- if (paddleOne == nil)
- paddleOne = CreateOffscreen(paddlePortRect);
-
- SetPort((GrafPtr)paddleOne);
-
- RGBForeColor(&black);
- PaintRect(&paddleOne->portRect);
-
- RGBForeColor(&green);
- PaintRect(&paddleRect);
-
- RGBForeColor(&lightgreen);
-
- PenSize(2,2);
- FrameRect(&paddleRect);
-
-
- if (paddleTwo == nil)
- paddleTwo = CreateOffscreen(paddlePortRect);
-
- SetPort((GrafPtr)paddleTwo);
-
- RGBForeColor(&black);
- PaintRect(&paddleTwo->portRect);
-
- RGBForeColor(&red);
- PaintRect(&paddleRect);
-
- RGBForeColor(&lightred);
-
- PenSize(2,2);
- FrameRect(&paddleRect);
- }
-
- void CreateBall(WindowPtr theWindow)
- {
- short width,height;
- Rect ballR;
-
-
- width = (theWindow->portRect.right - theWindow->portRect.left) / HeightCalc;
- height = (theWindow->portRect.bottom - theWindow->portRect.top) / HeightCalc;
-
- SetRect(&ballRect, 0,0, width + (BALLSPEED << 1) + 3, height + (BALLSPEED << 1) + 3);
- SetRect(&ballR, 0 + BALLSPEED + 2, 0 + BALLSPEED + 2, width, height);
-
- if (ball == nil)
- ball = CreateOffscreen(ballRect);
-
- SetPort((GrafPtr)ball);
-
- RGBForeColor(&black);
- PaintRect(&ball->portRect);
-
- RGBForeColor(&blue);
-
- PaintOval(&ballR);
-
- RGBForeColor(&lightblue);
-
- PenSize(1, 1);
- FrameOval(&ballR);
-
- OffsetRect(&ballRect, 20, 20);
- }
-
-
- void InitPong(WindowPtr theWindow)
- {
- GrafPtr oldPort;
-
-
- GetPort(&oldPort);
-
- if (pongGWorld == nil)
- pongGWorld = CreateOffscreen(theWindow->portRect);
-
- if (pongGWorld == nil)
- {
- DebugStr("\pOh shit again!");
- return;
- }
-
- SetPort(theWindow);
-
- RGBForeColor(&black);
- PaintRect(&theWindow->portRect);
-
- CreatePaddles(theWindow);
- CreateBall(theWindow);
-
- PenNormal();
-
- pongSpeed.h = BALLSPEED;
- pongSpeed.v = BALLSPEED;
-
- pongWindow = theWindow;
-
- SetPort(oldPort);
- }
-
-
- void DisposePong(void)
- {
- if (ball != nil)
- DisposePtr((Ptr)ball);
- ball = nil;
- if (paddleOne != nil)
- DisposePtr((Ptr)paddleOne);
- paddleOne = nil;
- if (paddleTwo != nil)
- DisposePtr((Ptr)paddleTwo);
- paddleTwo = nil;
- if (pongGWorld != nil)
- DisposePtr((Ptr)pongGWorld);
- pongGWorld = nil;
- }
-
-
- #pragma mark -
-
-
- void DoBreakOut(WindowPtr theWindow, EventRecord * theEvent)
- {
- UpdateBreakoutPaddle(theWindow, theEvent);
- UpdateBreakoutBall(theWindow);
- }
-
-
- void UpdateBreakoutPaddle(WindowPtr theWindow, EventRecord * theEvent)
- {
- Boolean handled = FALSE;
-
-
- if(KeyIsDown(aKey))
- {
- if(boPaddleRect.left > 0)
- OffsetRect(&boPaddleRect, -PADDLESPEED, 0);
- handled = TRUE;
- }
- if(KeyIsDown(sKey))
- {
- if(boPaddleRect.right < theWindow->portRect.right)
- OffsetRect(&boPaddleRect, PADDLESPEED, 0);
- handled = TRUE;
- }
- if(KeyIsDown(fourKey))
- {
- if(boPaddleRect.left > 0)
- OffsetRect(&boPaddleRect, -PADDLESPEED, 0);
- handled = TRUE;
- }
- if(KeyIsDown(sixKey))
- {
- if(boPaddleRect.right < theWindow->portRect.right)
- OffsetRect(&boPaddleRect, PADDLESPEED, 0);
- handled = TRUE;
- }
- if (KeyIsDown(fiveKey) || KeyIsDown(threeKey) || KeyIsDown(oneKey))
- handled = TRUE;
-
-
- RGBBackColor(&white);
- CopyBits( &((GrafPtr)boPaddle)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &boPaddle->portRect,
- &boPaddleRect,
- 0,
- NULL);
-
- if (theEvent != nil && handled)
- {
- theEvent->what = nullEvent;
- }
- }
-
- void UpdateBreakoutBall(WindowPtr theWindow)
- {
- Rect where;
- short center1,center2,offset1,offset2, offset3;
-
-
- if(boBallRect.right >= theWindow->portRect.right)
- boSpeed.h *= -1;
- if(boBallRect.left <= 0)
- boSpeed.h *= -1;
- if(boBallRect.bottom >= theWindow->portRect.bottom)
- boSpeed.v *= -1;
- if(boBallRect.top <= 0)
- boSpeed.v *= -1;
-
- if(SectRect(&boBallRect, &boPaddleRect, &where))
- {
- if(boBallRect.bottom-boSpeed.v <= boPaddleRect.top)
- {
- center1 = where.right / 2;
- center2 = boPaddleRect.right / 2;
-
- offset1 = (theWindow->portRect.right / (WidthCalc*5))+boPaddleRect.left;
- offset2 = offset1+(theWindow->portRect.right / (WidthCalc*5));
- offset3 = offset2+(theWindow->portRect.right / (WidthCalc*5));
-
- if(center1 < offset1)
- boSpeed.v *= -1,
- boSpeed.h++;
-
- if(center1 > offset1 && center1 < offset2)
- boSpeed.v *= -1;
-
- if(center1 > offset2 && center1 < offset3)
- boSpeed.v *= -1,
- boSpeed.h--;
- }
- }
-
- if(boSpeed.h > BALLSPEED)
- boSpeed.h = BALLSPEED;
- if(boSpeed.v > BALLSPEED)
- boSpeed.v = BALLSPEED;
- if(boSpeed.h < -BALLSPEED)
- boSpeed.h = -BALLSPEED;
- if(boSpeed.v < -BALLSPEED)
- boSpeed.v = -BALLSPEED;
-
- OffsetRect(&boBallRect, boSpeed.h, boSpeed.v);
-
- RGBBackColor(&white);
- CopyBits( &((GrafPtr)boBall)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &boBall->portRect,
- &boBallRect,
- 0,
- NULL);
-
- CheckCollide(theWindow);
- }
-
-
- void CheckCollide(WindowPtr theWindow)
- {
- short i,j;
- Rect where;
-
- for(i = 0;i < BricksH; i++)
- {
- for(j = 0;j < BricksV; j++)
- {
- if(BrickState[i][j] != kHit)
- {
- if(SectRect(&boBallRect, &Bricks[i][j], &where))
- {
- SysBeep(9);
- BrickState[i][j] = kHit;
- boSpeed.v *= -1;
- DrawBricks(theWindow);
- return;
- }
- }
- }
- }
- }
-
-
- void CreateBreakoutPaddle(WindowPtr theWindow)
- {
- short width,height;
- Rect paddlePortRect, paddleRect;
-
-
- width = (theWindow->portRect.right - theWindow->portRect.left) / WidthCalc;
- height = (theWindow->portRect.bottom - theWindow->portRect.top) / HeightCalc;
-
- SetRect(&paddlePortRect, 0, 0, width + (PADDLESPEED << 1) + 4, height);
- boPaddleRect = paddlePortRect;
-
- OffsetRect(&boPaddleRect, theWindow->portRect.right/2-width,theWindow->portRect.bottom-height*2);
-
- SetRect(&paddleRect, PADDLESPEED + 1, 0, width + PADDLESPEED + 1, height);
-
- if (boPaddle == nil)
- boPaddle = CreateOffscreen(paddlePortRect);
-
- if (boPaddle == nil)
- {
- DebugStr("\pboPaddle nil");
- return;
- }
-
- RGBForeColor(&green);
- PaintRect(&paddleRect);
-
- RGBForeColor(&lightgreen);
-
- PenSize(2,2);
- FrameRect(&paddleRect);
- }
-
-
- void CreateBreakoutBall(WindowPtr theWindow)
- {
- short width,height;
- Rect ballR;
-
-
- width = (theWindow->portRect.right - theWindow->portRect.left) / HeightCalc;
- height = (theWindow->portRect.bottom - theWindow->portRect.top) / HeightCalc;
-
- SetRect(&boBallRect, 0,0, width + (BALLSPEED << 1) + 3, height + (BALLSPEED << 1) + 3);
- SetRect(&ballR, 0 + BALLSPEED + 2, 0 + BALLSPEED + 2, width, height);
-
- if (boBall == nil)
- boBall = CreateOffscreen(boBallRect);
-
- if (boBall == nil)
- {
- DebugStr("\pboBall nil");
- return;
- }
-
- RGBForeColor(&blue);
-
- PaintOval(&ballR);
-
- RGBForeColor(&lightblue);
-
- PenSize(1, 1);
- FrameOval(&ballR);
-
- OffsetRect(&boBallRect, 20, BrickHeight * 5);
- }
-
-
- void CreateBricks(WindowPtr window)
- {
- short width, height;
- short i,j;
-
- width = BrickWidth = (window->portRect.right - BricksH) / BricksH;
- height = BrickHeight = window->portRect.bottom / HeightCalc;
-
- SetRect(&brickRect, 0, 0, width, height);
-
- if (brickWorld == nil)
- brickWorld = CreateOffscreen(brickRect);
-
- if (brickWorld == nil)
- {
- DebugStr("\pbricks nil");
- return;
- }
-
- RGBForeColor(&red);
- PaintRect(&brickRect);
-
- PenSize(2,2);
- RGBForeColor(&lightred);
- FrameRect(&brickRect);
-
- for(i = 0;i < BricksH; i++)
- {
- for(j = 0;j < BricksV; j++)
- {
- SetRect(&Bricks[i][j], i+1+i*BrickWidth, j+1+j*BrickHeight, i+1+i*BrickWidth+BrickWidth, j+1+j*BrickHeight+BrickHeight);
- BrickState[i][j] = kSafe;
- }
- }
- }
-
- void DrawBricks(WindowPtr theWindow)
- {
- short i,j;
- Rect where;
-
- for(i = 0;i < BricksH; i++)
- for(j = 0;j < BricksV; j++)
- {
- if(BrickState[i][j] != kHit)
- {
- SetRect(&where, i+1+i*BrickWidth, j+1+j*BrickHeight, i+1+i*BrickWidth+BrickWidth, j+1+j*BrickHeight+BrickHeight);
- RGBBackColor(&white);
- CopyBits( &((GrafPtr)brickWorld)->portBits,
- &((GrafPtr)theWindow)->portBits,
- &brickWorld->portRect,
- &where,
- 0,
- NULL);
- }
- else
- {
- SetPort((GrafPtr)theWindow);
- RGBForeColor(&black);
- PaintRect(&Bricks[i][j]);
- }
- }
- }
-
-
- void InitBreakOut(WindowPtr theWindow)
- {
- GrafPtr oldPort;
-
-
- GetPort(&oldPort);
-
- if (boGWorld == nil)
- boGWorld = CreateOffscreen(theWindow->portRect);
-
- if (boGWorld == nil)
- {
- DebugStr("\pboGWorld nil");
- return;
- }
-
- SetPort(theWindow);
-
- RGBForeColor(&black);
- PaintRect(&theWindow->portRect);
-
- CreateBricks(theWindow);
- CreateBreakoutPaddle(theWindow);
- CreateBreakoutBall(theWindow);
- DrawBricks(theWindow);
-
- PenNormal();
-
- boSpeed.h = BALLSPEED;
- boSpeed.v = BALLSPEED;
-
- breakoutWindow = theWindow;
-
- SetPort(oldPort);
- }
-
-
- void DisposeBreakOut(void)
- {
- if (boBall != nil)
- DisposePtr((Ptr)boBall);
- boBall = nil;
- if (boPaddle != nil)
- DisposePtr((Ptr)boPaddle);
- boPaddle = nil;
- if (boGWorld != nil)
- DisposePtr((Ptr)boGWorld);
- boGWorld = nil;
- }
-